﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinStudentMS
{
    public partial class ClassListForm : Form
    {
        public ClassListForm()
        {
            InitializeComponent();
        }

        private Action reLoad = null;

        private void ClassListForm_Load(object sender, EventArgs e)
        {
            InitGrades();
            InitAllClasses();
        }

        private void InitAllClasses()
        {
            string sql = "select ClassId,ClassName,GradeName,Remark from ClassInfo c inner join GradeInfo g on c.GradeId=g.GradeId";
            DataTable dataTable = SqlHelper.GetDataTable(sql);
            dgvClassList.DataSource = dataTable;
        }

        private void InitGrades()
        {
            string sql = "select GradeId,GradeName from GradeInfo";
            DataTable dataTable = SqlHelper.GetDataTable(sql);
            DataRow dataRow = dataTable.NewRow();
            dataRow["GradeId"] = 0;
            dataRow["GradeName"] = "请选择";
            //dataTable.Rows.Add(dataRow);
            dataTable.Rows.InsertAt(dataRow, 0);
            cboGrades.DataSource = dataTable;
            cboGrades.DisplayMember = "GradeName";
            cboGrades.ValueMember = "GradeId";
            cboGrades.SelectedIndex = 0;
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            int gradeId = (int) cboGrades.SelectedIndex;
            string className = txtClassName.Text.Trim();
            string sql = "select ClassId,ClassName,GradeName,Remark from ClassInfo c inner join GradeInfo g on c.GradeId=g.GradeId";
            sql += " where 1=1";
            if (gradeId > 0)
            {
                sql += " and c.GradeId=@GradeId";
            }
            if (!string.IsNullOrEmpty(className))
            {
                sql += " and ClassName like @ClassName";
            }

            SqlParameter[] paras =
            {
                new SqlParameter("@GradeId", gradeId),
                new SqlParameter("@ClassName", "%" + className + "%")
            };
            DataTable dataTable = SqlHelper.GetDataTable(sql, paras);
            dgvClassList.DataSource = dataTable;
        }

        private void dgvClassList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                DataGridViewCell cell = dgvClassList.Rows[e.RowIndex].Cells[e.ColumnIndex];
                DataRow dataRow = (dgvClassList.Rows[e.RowIndex].DataBoundItem as DataRowView).Row;
                int classId = (int)dataRow["ClassId"];
                if (cell is DataGridViewLinkCell && cell.FormattedValue.ToString() == "修改")
                {
                    reLoad = InitAllClasses;
                    EditClassForm editClassForm = new EditClassForm();
                    editClassForm.Tag = new TagObject()
                    {
                        EditId = classId,
                        ReLoad = reLoad
                    };
                    editClassForm.MdiParent = this.MdiParent;
                    editClassForm.Show();
                }
                else if (cell is DataGridViewLinkCell && cell.FormattedValue.ToString() == "删除")
                {
                    if (MessageBox.Show("您确定要删除该班级及其相关学生的信息吗？", "删除班级提示", MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        string delClass = "delete from ClassInfo where ClassId=@ClassId";
                        string delStudent = "delete from StudentInfo where ClassId=@ClassId";
                        SqlParameter[] para =
                        {
                        new SqlParameter("@ClassId", classId)
                        };
                        List<CommandInfo> listComs = new List<CommandInfo>();
                        CommandInfo comStudent = new CommandInfo()
                        {
                            CommandText = delStudent,
                            IsProc = false,
                            Parameters = para
                        };
                        listComs.Add(comStudent);
                        CommandInfo comClass = new CommandInfo()
                        {
                            CommandText = delClass,
                            IsProc = false,
                            Parameters = para
                        };
                        listComs.Add(comClass);
                        bool result = SqlHelper.ExecuteTrans(listComs);
                        if (result)
                        {
                            MessageBox.Show("该班级信息删除成功!", "班级学生提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            DataTable dataTable = (DataTable)dgvClassList.DataSource;
                            dataTable.Rows.Remove(dataRow);
                            dgvClassList.DataSource = dataTable;
                        }
                        else
                        {
                            MessageBox.Show("该班级信息删除成功!", "班级学生提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            List<int> listId = new List<int>();
            for (int i = 0; i < dgvClassList.Rows.Count; i++)
            {
                DataGridViewCheckBoxCell cell = dgvClassList.Rows[i].Cells["colCheck"] as DataGridViewCheckBoxCell;
                bool check = Convert.ToBoolean(cell.Value);
                if (check)
                {
                    DataRow dr = (dgvClassList.Rows[i].DataBoundItem as DataRowView).Row;
                    int classId = (int)dr["ClassId"];
                    listId.Add(classId);
                }
            }
            if (listId.Count == 0)
            {
                MessageBox.Show("请选择删除的班级信息!", "删除班级提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                if (MessageBox.Show("您确定要删除这些班级及其相关的学生信息吗？", "删除班级提示", MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    string delClass = "delete from ClassInfo where ClassId=@ClassId";
                    string delStudent = "delete from StudentInfo where ClassId=@ClassId";
                    List<CommandInfo> listComs = new List<CommandInfo>();
                    foreach (int id in listId)
                    {
                        SqlParameter[] para =
                        {
                            new SqlParameter("@ClassId", id)
                        };
                        CommandInfo comStudent = new CommandInfo()
                        {
                            CommandText = delStudent,
                            IsProc = false,
                            Parameters = para
                        };
                        listComs.Add(comStudent);
                        CommandInfo comClass = new CommandInfo()
                        {
                            CommandText = delClass,
                            IsProc = false,
                            Parameters = para
                        };
                        listComs.Add(comClass);
                    }
                    
                    bool result = SqlHelper.ExecuteTrans(listComs);
                    if (result)
                    {
                        MessageBox.Show("这些班级信息及其相关学生信息删除成功!", "删除班级提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        DataTable dataTable = (DataTable)dgvClassList.DataSource;
                        string idStr = string.Join(",", listId);
                        DataRow[] dataRow = dataTable.Select("ClassId in(" + idStr + ")");
                        foreach (DataRow row in dataRow)
                        {
                            dataTable.Rows.Remove(row);
                        }
                        dgvClassList.DataSource = dataTable;
                    }
                    else
                    {
                        MessageBox.Show("这些班级信息删除成功!", "删除班级提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }
    }
}
